import itertools

class Solution(object):
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp = list(range(n+1))

        for i in range(n):
            for j in itertools.count(2, 1):
                square = j**2
                index = i + square
                if index > n:
                    break
                dp[index] = min(dp[index], dp[i]+1)
        return dp[n]

s = Solution()
print(s.numSquares(9975))